package vn.edu.saigontech.ncms.service.impl;

import java.sql.Time;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashSet;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.model.teaching.OfferingChecking;
import vn.edu.saigontech.ncms.service.OfferingCheckingService;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.TeacherService;
import vn.edu.saigontech.ncms.util.DateUtil;

public class OfferingCheckingServiceImpl extends HibernateGenericService<OfferingChecking, Long> implements OfferingCheckingService {

	@Override
	public OfferingChecking add(Long offeringId, Date date, Time startTime, Time endTime, Long teacherId, String supervisor, String comment) {
		Offering offering = OfferingService.INSTANCE.findById(offeringId);
		OfferingChecking offeringChecking = new OfferingChecking();
		offeringChecking.setDate(date);
		offeringChecking.setStartTime(startTime);
		offeringChecking.setEndTime(endTime);
		offeringChecking.setTeacher(TeacherService.INSTANCE.findById(teacherId));
		offeringChecking.setSupervisor(supervisor);
		offeringChecking.setComment(comment);
		offeringChecking.setOffering(offering);
		super.persist(offeringChecking);
		return offeringChecking;
	}

	@Override
	public OfferingChecking edit(Long id, Date date, Time startTime, Time endTime, Long teacherId, String supervisor, String comment) {
		OfferingChecking offeringChecking = super.findById(id);
		offeringChecking.setDate(date);
		offeringChecking.setStartTime(startTime);
		offeringChecking.setEndTime(endTime);
		offeringChecking.setTeacher(TeacherService.INSTANCE.findById(teacherId));
		offeringChecking.setSupervisor(supervisor);
		offeringChecking.setComment(comment);
		super.persist(offeringChecking);
		return offeringChecking;
	}

	@Override
	public Collection<OfferingChecking> find(Offering offering, Teacher teacher, int month, int year) {
		Collection<OfferingChecking> offeringCheckings = offering.getOfferingCheckings(teacher);
		Collection<OfferingChecking> result = new LinkedHashSet<OfferingChecking>();
		for(OfferingChecking offeringChecking : offeringCheckings) {
			if(DateUtil.getMonth(offeringChecking.getDate()) == month &&
			   DateUtil.getYear(offeringChecking.getDate()) == year) {
				result.add(offeringChecking);
			}
		}
		return result;
	}
}